<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Init Model
 *
 */


class Category_model extends CI_Model {

	function __construct(){
		parent::__construct();

		$config = array('table'=>'category');
		$this->load->library('mahana_hierarchy');
		$this->mahana_hierarchy->initialize($config);
	}


	function breadcrumb($id=0){
		$r = $this->mahana_hierarchy->get_ancestors($id);
		return $r;
		//var_dump($r); die();
		//$this->mahana_hierarchy->resync();
	}

	//--GET CHILD RECORDS--//
	public function getChild($parent_id = 0,$limit=1, $data=FALSE){

		//die($parent_id);
		if($data == TRUE){
			$sql = "SELECT C.*, count(C.id) as NO FROM category AS C
			INNER JOIN term AS T ON C.id  = T.value and (T.term='category' or T.term='keyword')
			WHERE C.parent_id = $parent_id group by C.id
			order by (NO) DESC limit $limit;";
			$rs = $this->db->query($sql)->result();
		}else{
			//$rs = $this->mahana_hierarchy->get_children($parent_id,$limit);
			$sql = "SELECT * FROM category WHERE parent_id = $parent_id  limit 0, $limit;";
			$rs = $this->db->query($sql)->result();
		}
		//print $sql; die();
		if($rs){
			return $rs;
		}

	}

	//--GET PARENT RECORDS--//
	public function getParent($parent_id=0){
		$rs = $this->mahana_hierarchy->get_ancestors($parent_id,false);
		if($rs){
			return $rs;
		}
	}

	//--GET CATEGORY BY PK--//
	public function getPK($catID){
		$sql="select * from category where id=$catID limit 1";

		$result = $this->db->query($sql)->result_array();
		if($result){
			return $result[0];
		}else{
			return false;
		}
	}

	//--Get Category--ypgov//
	function getCetegory($parent_id=0, $limit=1, $data=FALSE){

		if($data == TRUE){

			$sql="SELECT C.id,name,slug, count(C.id) as NO from category C
			INNER JOIN term T on C.id = T.value and T.term = 'category' and C.parent_id = $parent_id GROUP BY C.id
			order by (NO) DESC limit 0,$limit";
		}
		elseif($data == FALSE){
			$sql="select * from category where parent_id=$parent_id order by (name) ASC limit 0,$limit ";
		}


		$result = $this->db->query($sql)->result();

		if(!$result){
			$sql = "select parent_id from category where id=$parent_id limit 1";
			$result = $this->db->query($sql)->row();
			$parent_id = $result->parent_id;
			$sql="select * from category where parent_id=$parent_id order by (name) ASC limit 0,$limit ";
			$result = $this->db->query($sql)->result();
		}

		if ($result){
			return $result;
		} else {
			return false;
		}

	}

	function getID_by_Name($str='', $limit = 1){
		$sql = "Select id from category where name like '%$str%' limit $limit";
		$query = $this->db->query($sql);
		
		if ($query->num_rows() > 0){
			$result = $query->result();
			return $result;
		} else {
			return false;
		}
	}
	
	//--Get Category ID by SLUG --//
	function getID($slug=''){
		$sql="select id from category where slug='$slug' limit 1";

		$result = $this->db->query($sql)->row();
		if($result){
			return $result->id;
		}else{
			return false;
		}
	}

	function getCategoryHTML($postID=0){
		$sql = "select id,value from term where (term='category' or term='keyword') and post_id = $postID order by (id) ASC";
		$result = $this->db->query($sql)->result();
		//die($sql);
		$link='';

		if($result){

			foreach ($result as $key){
				$c = $this->_getCategory($key->value);
				if(isset($c) && $c != '0'){
					$n = $c->name;
					$u = base_url().seoUrl(Location())."/".$c->slug;
					$link.="<a class='' href=$u>". $n." </a>, ";
				}
			}

			$length = strlen(trim($link));
			$link = substr_replace($link, '', $length - 1, $length);
			return $link;

		}else{
			$link = 'Category not found..';
			return $link;
		}
	}
	function _getCategory($catID){
		$sql="select id,name,slug from category where id=$catID limit 1";
		$query = $this->db->query($sql);
		//print_r($query->num_rows()); die();
		//$result = $query->row();
		if ($query->num_rows() > 0){
			return $query->row();
		} else {
			return false;
		}
	}

	//-- Get City by State --rahul--//
	function getChildbyparent($parentID = null){

		$this->db->select('*');

		if($parentID != NULL){
			$this->db->where('parent_id', $parentID);
		}
		$query = $this->db->get('category');


		$subcategory = array();

		if($query->result()){
			foreach ($query->result() as $child) {
				$subcategory[$child->id] = $child->name;
			}
			return $subcategory;

		}else{
			return FALSE;
		}


	}

	//--Get Keyword --rahul--//
	function getKeywordbysubCategory($childID = null){

		$this->db->select('*');

		if($childID != NULL){
			$this->db->where('parent_id', $childID);
		}
		$query = $this->db->get('category');


		$keywords = array();

		if($query->result()){
			foreach ($query->result() as $keyword) {
				$keywords[$keyword->id] = $keyword->name;
			}
			return $keywords;

		}else{
			return FALSE;
		}


	}


}